#!/usr/bin/env python
# coding=utf-8

import glob
import codecs
import random

# Keep KEEP_LINES_NUMBER lines in each *.txt
KEEP_LINES_NUMBER = 3

def main():
	# get filename list
	filenameList = tuple(glob.glob(r'*.txt'))
	dicIdList = tuple(fn[:8] for fn in filenameList)

	# make LoopBack.cfg file
	loopBackCfg = open('LoopBack.cfg', 'w')
	for dicId in dicIdList:
		loopBackCfg.write('CHN\t%s\n'%(dicId,))
	loopBackCfg.close()

	# create *.DicID file
	for dicId in dicIdList:
		dicIdFile = open(dicId + '.DicID', 'w')
		dicIdFile.write('0x' + dicId + '\n')
		dicIdFile.close()
		
	# keep several lines in each *.txt
	for fn in filenameList:
		f = codecs.open(fn, 'r', 'gbk')
		lines = list(f.readlines())
		f.close()
		random.shuffle(lines)
		f = codecs.open(fn, 'w', 'gbk')
		f.writelines(lines[:KEEP_LINES_NUMBER])
		f.close()

if __name__ == '__main__':
	main()

